﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
' http://dnntweet.codeplex.com
'
' Copyright (c) 2010, Will Strohl
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this 
' software and associated documentation files (the "Software"), to deal in the Software 
' without restriction, including without limitation the rights to use, copy, modify, 
' merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
' permit persons to whom the Software is furnished to do so, subject to the following 
' conditions:
'
' The above copyright notice and this permission notice shall be included in all copies 
' or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
' OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Services.Exceptions
Imports System.Web
Imports System.Web.SessionState
Imports WillStrohl.Modules.Twitter.TwitterController

Namespace WillStrohl.Modules.Twitter

    Partial Public Class SettingsHandler
        Implements IHttpHandler, IRequiresSessionState

#Region " Private Member "

        Private p_TabModuleId As Integer = Null.NullInteger
        Private p_ModuleId As Integer = Null.NullInteger

        Private p_TwitterConsumerKey As String = Null.NullString
        Private p_TwitterConsumerSecret As String = Null.NullString
        Private p_TwitterSearchTerms As String = Null.NullString
        Private p_TwitterRefreshRate As Integer = Null.NullInteger
        Private p_jQueryHosted As Nullable(Of Boolean) = Nothing
        Private p_jQueryHostedUrl As String = Null.NullString
        Private p_jQueryUIHosted As Nullable(Of Boolean) = Nothing
        Private p_jQueryUIHostedUrl As String = Null.NullString
        Private p_jQueryUITheme As String = Null.NullString
        Private p_SettingsGlobal As Nullable(Of Boolean) = Nothing

#End Region

#Region " Protected Properties "

        Protected ReadOnly Property TabModuleId() As Integer
            Get
                Return Me.p_TabModuleId
            End Get
        End Property

        Protected ReadOnly Property ModuleId() As Integer
            Get
                Return Me.p_ModuleId
            End Get
        End Property

        Protected ReadOnly Property TwitterConsumerKey() As String
            Get
                Return Me.p_TwitterConsumerKey
            End Get
        End Property

        Protected ReadOnly Property TwitterConsumerSecret() As String
            Get
                Return Me.p_TwitterConsumerSecret
            End Get
        End Property

        Protected ReadOnly Property TwitterSearchTerms() As String
            Get
                Return Me.p_TwitterSearchTerms
            End Get
        End Property

        Protected ReadOnly Property TwitterRefreshRate() As Integer
            Get
                Return Me.p_TwitterRefreshRate
            End Get
        End Property

        Protected ReadOnly Property jQueryHosted() As Boolean
            Get
                Return Me.p_jQueryHosted.Value
            End Get
        End Property

        Protected ReadOnly Property jQueryHostedUrl() As String
            Get
                Return Me.p_jQueryHostedUrl
            End Get
        End Property

        Protected ReadOnly Property jQueryUIHosted() As Boolean
            Get
                Return Me.p_jQueryUIHosted.Value
            End Get
        End Property

        Protected ReadOnly Property jQueryUIHostedUrl() As String
            Get
                Return Me.p_jQueryUIHostedUrl
            End Get
        End Property

        Protected ReadOnly Property jQueryUITheme() As String
            Get
                Return Me.p_jQueryUITheme
            End Get
        End Property

        Protected ReadOnly Property SettingsGlobal() As Boolean
            Get
                Return Me.p_SettingsGlobal.Value
            End Get
        End Property

#End Region

        Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

            Try

                context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
                context.Response.ContentType = "text/plain"

                If Not context.Request.QueryString("tmid") Is Nothing Then
                    Me.p_TabModuleId = Integer.Parse(context.Request.QueryString("tmid").ToString, Globalization.NumberStyles.Integer)
                Else
                    Throw New ArgumentNullException("tmid parameter missing")
                End If

                If Not context.Request.QueryString("mid") Is Nothing Then
                    Me.p_ModuleId = Integer.Parse(context.Request.QueryString("mid").ToString, Globalization.NumberStyles.Integer)
                Else
                    Throw New ArgumentNullException("mid parameter missing")
                End If

                If Not context.Request.QueryString("tck") Is Nothing Then
                    Me.p_TwitterConsumerKey = context.Request.QueryString("tck").ToString
                    'Else
                    '    Throw New ArgumentNullException("tck parameter missing")
                End If

                If Not context.Request.QueryString("tcs") Is Nothing Then
                    Me.p_TwitterConsumerSecret = context.Request.QueryString("tcs").ToString
                    'Else
                    '    Throw New ArgumentNullException("tcs parameter missing")
                End If

                If Not context.Request.QueryString("jh") Is Nothing Then
                    Me.p_jQueryHosted = Boolean.Parse(context.Request.QueryString("jh").ToString)
                Else
                    Throw New ArgumentNullException("jh parameter missing")
                End If

                If Me.jQueryHosted Then

                    If Not context.Request.QueryString("jhu") Is Nothing Then
                        Me.p_jQueryHostedUrl = context.Request.QueryString("jhu").ToString
                    Else
                        Throw New ArgumentNullException("jhu parameter missing")
                    End If

                    If Not context.Request.QueryString("juih") Is Nothing Then
                        Me.p_jQueryUIHosted = Boolean.Parse(context.Request.QueryString("juih").ToString)
                    Else
                        Throw New ArgumentNullException("juih parameter missing")
                    End If

                End If

                If Not context.Request.QueryString("juihu") Is Nothing Then
                    Me.p_jQueryUIHostedUrl = context.Request.QueryString("juihu").ToString
                Else
                    Throw New ArgumentNullException("juihu parameter missing")
                End If

                If Not context.Request.QueryString("juit") Is Nothing Then
                    Me.p_jQueryUITheme = context.Request.QueryString("juit").ToString
                Else
                    Throw New ArgumentNullException("juit parameter missing")
                End If

                If Not context.Request.QueryString("sg") Is Nothing Then
                    Me.p_SettingsGlobal = Boolean.Parse(context.Request.QueryString("sg").ToString)
                Else
                    Throw New ArgumentNullException("sg parameter missing")
                End If

                If Not context.Request.QueryString("trr") Is Nothing Then
                    Me.p_TwitterRefreshRate = Integer.Parse(context.Request.QueryString("trr").ToString, Globalization.NumberStyles.Integer)
                Else
                    Throw New ArgumentNullException("trr parameter missing")
                End If

                If Not context.Request.QueryString("tst") Is Nothing Then
                    Me.p_TwitterSearchTerms = context.Request.QueryString("tst").ToString
                Else
                    Throw New ArgumentNullException("tst parameter missing")
                End If

                SaveSettings()

                context.Response.Write("true")

            Catch ex As Exception
                LogException(ex)
                If IsEditMode() Then
                    context.Response.Write(String.Concat(ex.Message, Environment.NewLine, ex.StackTrace))
                    context.Response.End()
                End If
            End Try

        End Sub

        Private Sub SaveSettings()

            Dim ctlModule As New Entities.Modules.ModuleController
            ctlModule.UpdateTabModuleSetting(Me.TabModuleId, SETTINGS_GLOBAL, Me.SettingsGlobal.ToString)

            DeleteSettings(ctlModule)

            If Me.SettingsGlobal Then

                ctlModule.UpdateModuleSetting(Me.ModuleId, TWITTER_CONSUMER_KEY, Me.TwitterConsumerKey)
                ctlModule.UpdateModuleSetting(Me.ModuleId, TWITTER_CONSUMER_SECRET_KEY, Me.TwitterConsumerSecret)
                ctlModule.UpdateModuleSetting(Me.ModuleId, TWITTER_SEARCH_TERMS_KEY, Me.TwitterSearchTerms)
                ctlModule.UpdateModuleSetting(Me.ModuleId, TWITTER_REFRESH_RATE_KEY, Me.TwitterRefreshRate.ToString)
                ctlModule.UpdateModuleSetting(Me.ModuleId, JQUERY_HOSTED_KEY, Me.jQueryHosted.ToString)
                If Me.jQueryHosted Then
                    ctlModule.UpdateModuleSetting(Me.ModuleId, JQUERY_HOSTED_URL_KEY, Me.jQueryHostedUrl)
                    ctlModule.UpdateModuleSetting(Me.ModuleId, JQUERYUI_HOSTED_URL_KEY, Me.jQueryUIHostedUrl)
                End If
                ctlModule.UpdateModuleSetting(Me.ModuleId, JQUERYUI_HOSTED_KEY, Me.jQueryUIHosted.ToString)
                ctlModule.UpdateModuleSetting(Me.ModuleId, JQUERYUI_THEME_KEY, Me.jQueryUITheme)

            Else

                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, TWITTER_CONSUMER_KEY, Me.TwitterConsumerKey)
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, TWITTER_CONSUMER_SECRET_KEY, Me.TwitterConsumerSecret)
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, TWITTER_SEARCH_TERMS_KEY, Me.TwitterSearchTerms)
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, TWITTER_REFRESH_RATE_KEY, Me.TwitterRefreshRate.ToString)
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, JQUERY_HOSTED_KEY, Me.jQueryHosted.ToString)
                If Me.jQueryHosted Then
                    ctlModule.UpdateTabModuleSetting(Me.TabModuleId, JQUERY_HOSTED_URL_KEY, Me.jQueryHostedUrl)
                    ctlModule.UpdateTabModuleSetting(Me.TabModuleId, JQUERYUI_HOSTED_URL_KEY, Me.jQueryUIHostedUrl)
                End If
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, JQUERYUI_HOSTED_KEY, Me.jQueryUIHosted.ToString)
                ctlModule.UpdateTabModuleSetting(Me.TabModuleId, JQUERYUI_THEME_KEY, Me.jQueryUITheme)

            End If

        End Sub

        Private Sub DeleteSettings(ByVal ctlModule As Entities.Modules.ModuleController)

            ctlModule.DeleteModuleSetting(Me.ModuleId, TWITTER_CONSUMER_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, TWITTER_CONSUMER_SECRET_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, TWITTER_SEARCH_TERMS_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, TWITTER_REFRESH_RATE_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, JQUERY_HOSTED_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, JQUERY_HOSTED_URL_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, JQUERYUI_HOSTED_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, JQUERYUI_HOSTED_URL_KEY)
            ctlModule.DeleteModuleSetting(Me.ModuleId, JQUERYUI_THEME_KEY)

            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, TWITTER_CONSUMER_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, TWITTER_CONSUMER_SECRET_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, TWITTER_SEARCH_TERMS_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, TWITTER_REFRESH_RATE_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, JQUERY_HOSTED_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, JQUERY_HOSTED_URL_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, JQUERYUI_HOSTED_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, JQUERYUI_HOSTED_URL_KEY)
            ctlModule.DeleteTabModuleSetting(Me.TabModuleId, JQUERYUI_THEME_KEY)

        End Sub

#Region " IHttpHandler Implementation "

        Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Get
                Return True
            End Get
        End Property

#End Region

    End Class

End Namespace